package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.BaseDao;
import com.project.dao.ILinkDao;

import java.sql.SQLException;
import java.time.LocalDate;

public class LinkDaoImp extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNum) {
        CutPageBean<LinkBean> cutBean=new CutPageBean();
        this.setConnection();
        try {
            ps=con.prepareStatement("select * from t_link limit ?,?");
            ps.setObject(1,(pageNum-1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);

            rs=ps.executeQuery();
            while (rs.next()){
                LinkBean linkBean=new LinkBean();
                linkBean.setId(rs.getInt("pk_linkId"));
                linkBean.setName(rs.getString("l_name"));
                linkBean.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
                linkBean.setPhone(rs.getString("l_phone"));
                cutBean.getList().add(linkBean);
            }

            ps= con.prepareStatement("select  count(*) c from t_link ");
            rs= ps.executeQuery();
            if (rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

        return cutBean;
    }
}
